/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>
Copyright (c) 2008 Pierre "delroth" Bourdon <root@delroth.is-a-geek.org>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
Error code when opening a file
	
	-1 : can't open the file
	-2 : can't create a xml whith the file
	-3 : the file is not a VTO:document
	-4 : the file is not a specified type document
	

*/

#include "global.h"
#include "doc_part.h"

#include <QFile>
#include <QTextStream>


TitledDocument::TitledDocument(const QString& t)
{
    title = t;
    licence = 0;
    progress = 0;
    difficult = 0;
}

const QString& TitledDocument::getTitle() const
{
    return title;
}

void TitledDocument::setTitle(const QString& t)
{
    title = t;
}

const QString& TitledDocument::getFilename() const
{
    return filename;
}

void TitledDocument::setFilename(const QString& fname)
{
    filename = fname;
}

void TitledDocument::setDifficult(int d)
{
    difficult = d;
}

void TitledDocument::setProgress(int p)
{
    progress = p;
}

void TitledDocument::setLicence(int l)
{
    licence = l;
}

int TitledDocument::getDifficult()
{
    return difficult;
}

int TitledDocument::getProgress()
{
    return progress;
}

int TitledDocument::getLicence()
{
    return licence;
}

///////////////////////////////////////////////////////////////////////

const QString& ContentDocument::getContents() const
{
    return this->contents;
}

void ContentDocument::setContents(const QString& c)
{
    contents = c;
}
///////////////////////////////////////////////////////////////////////

Part::Part(const QString& title) : TitledDocument(title), ContentDocument()
{
}

Part::Part(const QDomElement& part)
{
    fromDomElement(part);
}

void Part::fromDomElement(const QDomElement& part)
{
	if(part.firstChildElement().tagName() == "title")
	setTitle(part.firstChildElement().text());
	else
		throw 300;

	if(part.lastChildElement().tagName() == "contents")
	setContents(part.lastChildElement().text());
	else
		throw 300;
}

void Part::fromFile(const QString& fname)
{
    
    QDomDocument doc("Part");
    QFile file(fname);

    if( file.open(QIODevice::ReadOnly) )
    {
		if( doc.setContent( &file ) )
		{
			file.close();
			
			QDomElement root = doc.documentElement();
			
			if(root.tagName() != "document")
				throw 300;
			
			if(root.firstChildElement().tagName() != "part")
				throw 300;
			
			filename = fname;
			fromDomElement(root.firstChildElement());
		}
		else
		{
			file.close();
			throw 301;
		}
    }
    else
		throw 400;
	
}

QDomElement Part::toDomElement(QDomDocument& doc) const
{
    QDomElement part = doc.createElement("part");
    QDomElement deTitle = doc.createElement("title");
    QDomElement deContents = doc.createElement("contents");

    deTitle.appendChild(doc.createCDATASection(title));
    deContents.appendChild(doc.createCDATASection(contents));
	
    part.appendChild(deTitle);
    part.appendChild(deContents);

    return part;
}

void Part::toFile(const QString& fname)
{
    QDomDocument doc("Part");
    QDomElement root = doc.createElement( "document" );    
    QDomElement part = toDomElement(doc);
    
    root.setAttribute("type", "part");
    root.appendChild(part);

    doc.appendChild( root );

    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
		QTextStream ts( &file );
		ts << doc.toString();
		file.close();

		filename = fname;
    }
    else
		throw 401;
}


///////////////////////////////////////////////////////////////////////

IntroducedDocument::IntroducedDocument(const QString& title) : TitledDocument(title)
{
	intro = new Part(QObject::tr("Introduction"));
	conclu = new Part(QObject::tr("Conclusion"));
}

IntroducedDocument::~IntroducedDocument()
{
	delete intro;
	delete conclu;
}

Part* IntroducedDocument::getIntro()
{
    return intro;
}

Part* IntroducedDocument::getConclu()
{
    return conclu;
}

Part* IntroducedDocument::getPart(int type)
{
	if(type == VTO::INTRO)
		return intro;
	else if(type == VTO::CONCLU)
		return conclu;
	else
		return NULL;
}

void IntroducedDocument::setIntro(const QString& i)
{
    intro->setContents(i);
}

void IntroducedDocument::setConclu(const QString& c)
{
    conclu->setContents(c);
}
